package com.sikaryofficial.backend.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import com.sikaryofficial.backend.mapper.StoreSearchResultItemMapper;
import com.sikaryofficial.backend.domain.entity.StoreSearchResultItem;
import com.sikaryofficial.backend.service.IStoreSearchResultItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;


@Service
public class StoreSearchResultItemService extends ServiceImpl<StoreSearchResultItemMapper, StoreSearchResultItem> implements IStoreSearchResultItemService {

    @Autowired
    private StoreSearchResultItemMapper resultItemMapper;

    /**
     * 批量插入或更新数据
     *
     * @param itemList 数据列表
     * @return 影响行数
     */
    @Override
    public int batchInsertOrUpdate(List<StoreSearchResultItem> itemList) {
        if (CollUtil.isEmpty(itemList)) {
            return 0;
        }
        // 使用google 对集合进行分批处理
        int processCount = 0;
        List<List<StoreSearchResultItem>> idPartition = Lists.partition(itemList, 2000);
        for (List<StoreSearchResultItem> items : idPartition) {
            processCount += resultItemMapper.batchInsertOrUpdate(items);
        }
        return processCount;
    }
}
